/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pe.basic.util;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

/**
 *
 * @author WCCAICEDO
 */
public final class MessageProvider {

    private static final Logger log = Logger.getLogger(MessageProvider.class);
    private ResourceBundle bundle;

    public ResourceBundle getBundle() {
        if (bundle == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            bundle = context.getApplication().getResourceBundle(context, "msgs");
        }
        return bundle;
    }

    public String getValue(String key) {
        String result = Constantes.VACIO;
        try {
            result = getBundle().getString(key);
        } catch (MissingResourceException e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
        return result;
    }

    private static class MessageProviderLoader {
        private static final MessageProvider INSTANCE = new MessageProvider();
    }

    private MessageProvider() {
        if (MessageProviderLoader.INSTANCE != null) {
            throw new IllegalStateException("Already instantiated");
        }
    }

    public static MessageProvider getInstance() {
        return MessageProviderLoader.INSTANCE;
    }
}
